<template>
  <div id="app">
    <el-card style="width: 500px; margin: auto">
      <div slot="header" class="clearfix">
        <span>修改密码</span>
      </div>
      <el-form :model="form" :rules="rules" ref="pass" label-width="120px" size="small">
        <el-form-item prop="password" label="原密码">
          <el-input show-password v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="newPassword" label="新密码">
          <el-input show-password v-model="form.newPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword" label="确认新密码">
          <el-input show-password v-model="form.confirmPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: right">
          <el-button type="primary" @click="save">确定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "PhyPassword",
  data() {
    return {
      form: {
        username: "",
        password: "",
        newPassword: "",
        confirmPassword: "",
      },
      tempPwd: "",
      user: localStorage.getItem("phy") ? JSON.parse(localStorage.getItem("phy")) : {},
      rules: {
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3,  message: '长度不少于3位', trigger: 'blur'}
        ],
        newPassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, message: '长度不少于3位', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3,  message: '长度不少于3位', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.form.username = this.user.username
  },
  methods: {
    save() {
      this.$refs.pass.validate((valid) => {
        if (valid) { //表单校验合法
          // this.request.post("/user/findPwd", this.form).then(res => {
          //   this.tempPwd = res
          //   console.log(this.tempPwd)
          //   //console.log(res)
          // })
          if(this.form.password !== this.user.password) {
            // console.log(this.form.password)
            // console.log(this.tempPwd)
            this.$message.error("原密码错误")
            return false
          }
          if (this.form.newPassword !== this.form.confirmPassword) {
            this.$message.error("2次输入的新密码不相同")
            return false
          }
          this.request.post("/physician/password", this.form).then(res => {
            if (res) {
              this.$message.success("修改成功")
              this.$router.push('/phyLogin')
              localStorage.removeItem("user")
            } else {
              this.$message.error("修改失败")
            }
          })
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
#app{
  padding-top: 30px;
  display:flex;
  justify-content: center;
  align-items: center;
}
</style>
